package deom10;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2023-04-14
 * Time: 6:53
 */
class ListNode{
    int val;
    ListNode next;
    public ListNode(int  val){
        this.val = val;
    }
}
public class Test01 {
    public boolean isPail(ListNode head){
        if(head == null){
            return false;
        }
        if(head.next == null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        //找到中间节点
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //反转中间节点以后的节点
        ListNode cur = slow.next;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //将反转后的节点与未反转的节点进行比较
        while (head != slow){
            if(head.val != slow.val){
                return false;
            }
            //当是偶数时
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
}
